import 'package:flutter/material.dart';
import 'package:interview/components/interview/interview_list.dart';
import 'package:interview/components/question/question_list.dart';
import 'package:interview/theme/app_colors.dart';


class SearchResult extends StatefulWidget {
  final AppColors theme;
  final String searchKeyword;
  final int initCurrentIndex;
  const SearchResult({super.key, required this.theme, required this.searchKeyword, required this.initCurrentIndex});

  @override
  State<SearchResult> createState() => _SearchResultState();
}

class _SearchResultState extends State<SearchResult> with SingleTickerProviderStateMixin {

  late TabController _tabController;
  late int currentIndex = 0;
  List<String> list = [ '试题' , '面经' ];


  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: list.length, vsync: this , initialIndex: widget.initCurrentIndex);
  }

  void onTabTapped(int index) {
    setState(() {
      currentIndex = index;
      if (_tabController.index != index) {
        _tabController.animateTo(index);
      }
    });
  }


  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Align(
          alignment: Alignment.centerLeft,
          child: TabBar(
            controller: _tabController,
            isScrollable: true,
            tabAlignment: TabAlignment.start,
            indicatorColor: widget.theme.contrastPure, // 选中状态下划线
            unselectedLabelStyle: TextStyle(
              fontWeight: FontWeight.normal,
              color: widget.theme.contrastMix1,
            ),
            labelStyle: TextStyle(
              fontWeight: FontWeight.bold,
              color: widget.theme.contrastPure,
            ),
            dividerHeight: 0,
            onTap: onTabTapped,
            tabs: list.map((tab){
              return Tab(
                text: tab,
                height: 36,
              );
            }).toList(),
          ),
        ),
        Expanded(child:
          TabBarView(
            controller: _tabController,
            children: [
              QuestionList(keyword: widget.searchKeyword),
              InterviewList(tab: widget.searchKeyword)
            ]
          )
        )

      ],
    );
  }


  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }
}